package com.octopus.yangj.customized.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import com.octopus.kernal.controller.AbstractValidator;
import com.octopus.yangj.generated.model.EmployeeStruct;

@Component
public class SignupValidator extends AbstractValidator {

    @Autowired
    private CustomizedEmployeeValidator customizedValidator;

	@Override
    public boolean supports(Class<?> aClass) {
        return EmployeeStruct.class.isAssignableFrom(aClass);
    }

	@Override
    public void validate(Object o, Errors errors) {
        EmployeeStruct entity = ((EmployeeStruct) o);
        // username
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.common.field.empty", new Object[] {messageSource.getMessage("label.user.field.username", new Object[] { }, LocaleContextHolder.getLocale())}, "Please specify a username");
        if (StringUtils.isNotBlank(entity.getUsername())&&(entity.getUsername().trim().length()> 10)) {
            errors.rejectValue("username", "error.common.field.length.greater", new Object[] {messageSource.getMessage("label.user.field.username", new Object[] { }, LocaleContextHolder.getLocale()), 10 }, "Please specify a valid value for username, not greater than 10");
        }
        // serial
        if (errors.hasFieldErrors("serial")) {
            errors.rejectValue("serial", "error.common.field.notnumber", new Object[] {messageSource.getMessage("label.user.field.serial", new Object[] { }, LocaleContextHolder.getLocale())}, "Please specify a valid number for serial");
        }
        if ((entity.getSerial()!= null)&&(String.valueOf(entity.getSerial()).length()> 100)) {
            errors.rejectValue("serial", "error.common.field.length.greater", new Object[] {messageSource.getMessage("label.user.field.serial", new Object[] { }, LocaleContextHolder.getLocale()), 100 }, "Please specify a valid value for serial, not greater than 100");
        }
        // email
        if (StringUtils.isNotBlank(entity.getEmail())&&(entity.getEmail().trim().length()> 100)) {
            errors.rejectValue("email", "error.common.field.length.greater", new Object[] {messageSource.getMessage("label.user.field.email", new Object[] { }, LocaleContextHolder.getLocale()), 100 }, "Please specify a valid value for email, not greater than 100");
        }
        // password
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.common.field.empty", new Object[] {messageSource.getMessage("label.user.field.password", new Object[] { }, LocaleContextHolder.getLocale())}, "Please specify a password");
        if (StringUtils.isNotBlank(entity.getPassword())&&(entity.getPassword().trim().length()> 100)) {
            errors.rejectValue("password", "error.common.field.length.greater", new Object[] {messageSource.getMessage("label.user.field.password", new Object[] { }, LocaleContextHolder.getLocale()), 100 }, "Please specify a valid value for password, not greater than 100");
        }
        // telphone
        if (StringUtils.isNotBlank(entity.getTelphone())&&(!(entity.getTelphone().trim().length() == 3))) {
            errors.rejectValue("telphone", "error.common.field.length.noequal", new Object[] {messageSource.getMessage("label.user.field.telphone", new Object[] { }, LocaleContextHolder.getLocale()), 3 }, "Please specify a valid value for telphone, equals to 3");
        }
        
        customizedValidator.validate(o, errors);
    }
}
